package com.dz.scrm.app.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.dz.scrm.app.config.*
import com.dz.scrm.mvp.model.entity.CachePhoneRecord
import com.dz.scrm.mvp.model.entity.EventMessage
import com.dz.scrm.utils.LoggerUtils.d
import com.dz.scrm.utils.TimerUtils
import com.dz.scrm.utils.getTenantId
import com.dz.scrm.utils.getUserId
import com.dz.scrm.utils.isHiddenPhoneNum
import me.jessyan.art.integration.EventBusManager
import java.util.*
import kotlin.math.abs

/**
 * @Description: 类作用描述
 * @Author: 黄海
 * @CreateDate: 2021-4-21 14:24
 * @UpdateUser: 黄海
 * @UpdateDate: 2021-4-21 14:24
 * @UpdateRemark: 更新说明
 */
class DZBroadCastReceiver : BroadcastReceiver() {

    // 响铃开始时间
    private var mStartRingTime = 0L

    // 通话开始时间
    private var mStartPhoneTime = 0L

    // 通话结束时间
    private var mEndPhoneTime = 0L

    // 是否打入电话
    private var inCall = false

    // 录音地址
    private var mRecordFileUrl = ""

    // 是否接通电话
    private var isConnect = false

    // 是否拒接
    private var isDisagree = false

    companion object {
        const val TIMER_DISCONNECTED = "DZBroadCastReceiver"
    }

    override fun onReceive(context: Context, intent: Intent) {

        when (intent.action) {
            BROADCAST_INCOMING_CALL -> {
                TimerUtils.getInstance().cancel(TIMER_DISCONNECTED)
                mStartRingTime = System.currentTimeMillis()
                mStartPhoneTime =0L
                mEndPhoneTime =0L
                mRecordFileUrl = ""
                isConnect = false
                inCall = true
                isDisagree = false
                d("接收到来电广播")
            }
            BROADCAST_OUTGOING_CALL -> {
                mStartRingTime = System.currentTimeMillis()
                mStartPhoneTime =0L
                mEndPhoneTime =0L
                TimerUtils.getInstance().cancel(TIMER_DISCONNECTED)
                mRecordFileUrl = ""
                inCall = false
                isConnect = false
                isDisagree = false
                d("接收到去电广播")
            }
            BROADCAST_CALL_CONNECTED -> {
                mStartPhoneTime = System.currentTimeMillis()
                d("电话接通广播")
            }
            BROADCAST_CALL_DISCONNECTED -> {
                d("电话挂断广播")
                mEndPhoneTime = System.currentTimeMillis()
                TimerUtils.getInstance().timer(TIMER_DISCONNECTED, 500) {
                    TimerUtils.getInstance().cancel(TIMER_DISCONNECTED)
                    val bean = CachePhoneRecord(
                        user_id = getUserId(),
                        tenant_id = getTenantId(),
                        uid = abs(UUID.randomUUID().hashCode()).toString(),
                        showPhoneNum = !isHiddenPhoneNum(),
                        // 响铃开始时间
                        phoneRingTime = mStartRingTime,
                        // 通话结束时间
                        phoneEndTime = mEndPhoneTime,
                        // 是否拒接
                        isDisagree = isDisagree
                    )

                    if (mStartPhoneTime == 0L) {
                        bean.phoneStartTime = mStartRingTime
                    } else {
                        bean.phoneStartTime = mStartPhoneTime
                    }

                    if (inCall) {
                        bean.phoneCallType = 4
                    } else {
                        bean.phoneCallType = 3
                    }
                    if (isConnect) {
                        bean.fileUrl = mRecordFileUrl
                        // 响铃时间，通话开始时间 - 响铃开始时间
                        bean.ringDurationTime = mStartPhoneTime - mStartRingTime
                        bean.phoneStatus = 1
                    } else {
                        //未接通电话
                        bean.ringDurationTime = mEndPhoneTime - mStartRingTime
                        bean.phoneStatus = 0
                    }
                    EventBusManager.getInstance()
                        .post(EventMessage(EVENT_BROADCAST_CALL_FINISH, bean))
                }
            }
            BROADCAST_CALL_REJECT -> {
                d("来电话拒接广播")
                isDisagree = true
            }
            BROADCAST_CALL_RECORD -> {
                isConnect = true
                mRecordFileUrl = intent.getStringExtra("extra_call_record_file") ?: ""
                d("通话录音完成广播: $mRecordFileUrl")
            }

            BROADCAST_ACTION_SIM_LOADED, BROADCAST_ACTION_SIM_ABSENT -> {
                d("SIM卡加载")
                EventBusManager.getInstance()
                    .post(EventMessage(EVENT_BROADCAST_ACTION_SIM_LOADED, ""))
            }
        }
    }
}